package com.example.yuecan.ui.info;


import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.example.yuecan.data.source.http.service.ApiService;
import com.example.yuecan.entity.MessageEvent;
import com.example.yuecan.entity.ResponseBean;
import com.example.yuecan.utils.RetrofitClient;
import com.example.yuecan.utils.network.SpKey;

import org.greenrobot.eventbus.EventBus;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.SPUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * Created by .
 */

public class ModifyInformationViewModel extends BaseViewModel {
    public ObservableField<String> userName = new ObservableField<>("");
    public ObservableField<String> password = new ObservableField<>("");
    public ObservableField<String> phone = new ObservableField<>("");

    public ModifyInformationViewModel(@NonNull Application application) {
        super(application);
        userName = new ObservableField<>(SPUtils.getInstance().getString(SpKey.NAMESS));
        password = new ObservableField<>(SPUtils.getInstance().getString(SpKey.PASS_WORD));
        phone = new ObservableField<>(SPUtils.getInstance().getString(SpKey.PHONE));
    }

    public BindingCommand ConfirmRevision = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            ConfirmRevision();
        }
    });

    //判断字符串是否为全为数字
    public static boolean isNumeric(String str){
        for (int i = str.length();--i>=0;){
            if (!Character.isDigit(str.charAt(i))){
                return false;
            }
        }
        return true;
    }

    private void ConfirmRevision() {
        if (TextUtils.isEmpty(userName.get())) {
            ToastUtils.showShort("请输入用户名！");
            return;
        }

        String iphone=phone.get();
        if (TextUtils.isEmpty(iphone)) {
            ToastUtils.showShort("请输入手机号！");
            return;
        }
        else if(iphone.length()!=11||!isNumeric(iphone)){
            ToastUtils.showShort("Attention:请输入正确的手机号！");
            return;
        }
        String pass=password.get();
        if (TextUtils.isEmpty(pass)) {
            ToastUtils.showShort("请输入密码！");
            return;
        }
        else if(pass.length()!=6||!isNumeric(pass)){
            ToastUtils.showShort("Attention:6位数字密码！");
            return;
        }

        RetrofitClient.getInstance().create(ApiService.class)
                .changeInformation(SPUtils.getInstance().getInt(SpKey.USER_ID), userName.get(),password.get(), phone.get(), SPUtils.getInstance().getInt(SpKey.IDENTITYS), 0)
                .compose(RxUtils.schedulersTransformer())
                .subscribe(new Observer<ResponseBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ResponseBean bean) {
                        if (bean.getSuccess() == 200) {
                            ToastUtils.showLong("修改成功");


                            SPUtils.getInstance().put(SpKey.NAMESS, userName.get());
                            SPUtils.getInstance().put(SpKey.PASS_WORD, password.get());
                            SPUtils.getInstance().put(SpKey.PHONE, phone.get());

                            EventBus.getDefault().post(new MessageEvent(MessageEvent.MODIFY_INFORMATION));

                            finish();
                        }
                    }


                    @Override
                    public void onError(Throwable e) {
                        ToastUtils.showLong(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

}
